package org.example.booksystem.service;

import org.example.booksystem.constants.Constants;
import org.example.booksystem.mapper.UserInfoMapper;
import org.example.booksystem.moudel.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;

@Service
public class UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper ;

    public boolean loginCheck(String userName, String password, HttpSession session) {
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return false ;
        }
        UserInfo userInfo = userInfoMapper.searchByName(userName);
        if (userInfo == null || !password.equals(userInfo.getPassword())){
            return false ;
        }
         // 清除掉一些不需要的属性
         userInfo.setPassword("");
        session.setAttribute(Constants.USERINFO_KEY, userInfo);

        return true  ;
    }
}
